/*
 * configure_adc.c
 *
 *  Created on: 2021闂備浇娉曢崰鎰板几婵犳艾绠柨鐕傛嫹5闂備浇娉曢崰鎰板几婵犳艾绠柨鐕傛嫹27闂備浇娉曢崰鎰板几婵犳艾绠柨鐕傛嫹
 *      Author: Administrator
 */

#include "spi_driver.h"
#include "xil_printf.h"
#include "../gpio/gwGpio.h"
#include "../utility.h"

static TaskHandle_t xSpiConfigureTask;

#if 1//Gu203闂佺粯顨呴悧濠傦耿閿燂拷
u32 hmc7044_config1_data[] = {
0x000001,
0x000000,
0x009600,
0x009700,
0x009800,
0x009900,
0x009a00,
0x009baa,
0x009caa,
0x009daa,
0x009eaa,
0x009f4d,
0x00a0df,
0x00a197,
0x00a203,
0x00a300,
0x00a400,
0x00a506,
0x00a61c,
0x00a700,
0x00a806,
0x00a900,
0x00ab00,
0x00ac20,
0x00ad00,
0x00ae08,
0x00af50,
0x00b004,
0x00b10d,
0x00b200,
0x00b300,
0x00b400,
0x00b500,
0x00b600,
0x00b700,
0x00b800,
0x000160,
0x000204,
0x00032f,//2f->37,濠电偞娼欓鍫ユ儊椤栨稓鈻旈柨鐔诲Г缁嬪鏁撻敓锟�;闂佺绻戞繛濠偽涚喊顢睱1.
0x00047f,
0x000546,
0x000600,
0x000700,
0x000800,
0x000901,
0x004610,
0x004710,
0x004800,
0x004912,
0x00501d,
0x005131,
0x005229,
0x005300,
0x005401,
0x003101,
0x003201,
0x003301,
0x003400,
0x00354b,
0x003600,
0x00370f,
0x003818,
0x003900,
0x003a00,
0x003b00,
0x000a00,
0x000b07,
0x000c07,
0x000d00,
0x000e07,
0x001466,
0x001501,
0x00160c,
0x00174f,//DAC婵烇絽娲︾换鍐拷鍨瀹曟劙鏁撻敓锟�,婵帗绋掗…鍫ヮ敇閿燂拷4F,7F=0.668V;
0x00180c,
0x001901,
0x001a08,
0x001b18,//婵帗绋掗…鍫ヮ敇閿燂拷18闂佹寧鍐婚幏锟�
0x001c02,
0x001d14,//闂佸憡甯掑Λ婵嬨�傞敓锟�
0x001e14,
0x001f02,
0x002008,
0x002101,
0x002200,
0x002608,//闂佸憡甯掑Λ婵嬨�傞敓锟�
0x002700,
0x002813,
0x002917,//PLL1闂佸憡鐟ラ崐浠嬫晸閽樺鏆曞ǎ鍥э躬瀹曪綁鏁撻敓锟�
0x002a00,
0x005a00,
0x005b02,
0x005c00,
0x005d03,
0x006400,
0x006500,
0x0070ff,
0x00711f,
0x00c8f3,//闂佺绻愰崢鏍姳閿燂拷
0x00c918,
0x00ca00,
0x00cb00,
0x00cc00,
0x00cd00,
0x00ce00,
0x00cf00,
0x00d003,
0x00d100,
0x00d2f3,//闂佺绻愰崢鏍姳閿燂拷
0x00d318,
0x00d400,
0x00d500,
0x00d600,
0x00d700,
0x00d800,
0x00d900,
0x00da03,
0x00db00,
0x00dcf3,//闂佺绻愰崢鏍姳閿燂拷
0x00dd18,
0x00de00,
0x00df00,
0x00e000,
0x00e100,
0x00e200,
0x00e300,
0x00e403,
0x00e500,
0x00e6f3,//闂佺绻愰崢鏍姳閿燂拷
0x00e718,
0x00e800,
0x00e900,
0x00ea00,
0x00eb00,
0x00ec00,
0x00ed00,
0x00ee03,
0x00ef00,
0x00f0f3,//闂佺绻愰崢鏍姳閿燂拷
0x00f101,//CN4
0x00f200,//CN4
0x00f300,
0x00f400,
0x00f500,
0x00f600,
0x00f700,
0x00f808,//3->8
0x00f900,
0x00fa73,//闂佺绻愰崢鏍姳閿燂拷
0x00fb00,
0x00fc02,
0x00fd00,
0x00fe00,
0x00ff00,
0x010000,
0x010101,
0x0102a8,//
0x010300,
0x0104f3,//CN6,f3
0x010501,//CN6 7044闂佸憡甯掑Λ婵嬨�傞崜浣瑰闁挎稑瀚弳顒勬煥濞戞ê顨欑紒鐙呯秮瀹曟粌顓奸崱妯烇拷1
0x010600,//婵炴垶鏌ㄩ鍛櫠閻樿鍙婇柨鐕傛嫹0
0x010700,
0x010800,
0x010900,
0x010a00,
0x010b00,
0x010c08,//婵炴垶鎸搁敃锝囩博鐎涙鈻旀い蹇撳閹风兘宕楅崗澶癸箓鏌涢幇顓犳噧缂佽鎷�01闂佹寧绋掗宥甃+100R闂佹寧鍐婚幏锟�03闂佹寧绋掗宥甃+50R;08闂佹寧绋掗悺鍦碢ECL;09:LVPECL+100R;0B:LVPECL+50R;10:LVDS;11:LVDS+100;13:LVDS+50R
0x010d00,
0x010e73,//7d,73
0x010f00,//闂備緡鍋呭畝鍛婄閿燂拷7闂佹寧绋戦懟顖氼嚕椤掑嫭鍤�闁告劏鏅滈ˇ褔姊洪弬銈嗗
0x011002,
0x011100,
0x011200,
0x011300,
0x011400,
0x011501,
0x0116a8,//A8闂佺尨鎷烽梺顐ｇ缁�锟�83
0x011700,
0x0118f3,//闂佺绻愰崢鏍姳閿燂拷
0x011920,
0x011a00,
0x011b00,
0x011c00,
0x011d00,
0x011e00,
0x011f00,
0x012003,
0x012100,
0x012273,//7D//闂佺绻愰崢鏍姳閿燂拷
0x012300,
0x012402,
0x012500,
0x012600,
0x012700,
0x012800,
0x012901,
0x012ab0,
0x012b00,
0x012cf3,//闂佺绻愰崢鏍姳閿燂拷
0x012d18,
0x012e00,
0x012f00,
0x013000,
0x013100,
0x013200,
0x013300,
0x013403,
0x013500,
0x0136f3,//闂佺绻愰崢鏍姳閿燂拷
0x013718,
0x013800,
0x013900,
0x013a00,
0x013b00,
0x013c00,
0x013d00,
0x013e03,
0x013f00,
0x0140f3,//闂佺绻愰崢鏍姳閿燂拷
0x014120,//婵炴垶鏌ㄩ鍛櫠閻樿绫嶉柨鐕傛嫹125M闂佹寧绋戦懟顖炲垂鎼搭澁绱ｉ柨鐕傛嫹18 125M,20 93.75M.
0x014200,
0x014300,
0x014400,
0x014500,
0x014600,
0x014700,
0x014810,//10 LVDS闂佺尨鎷烽梺顐ｇ缁�锟�03 CML+50R
0x014900,
0x014af2,
0x014b18,
0x014c00,
0x014d00,
0x014e00,
0x014f00,
0x015000,
0x015100,
0x015210,
0x015300
};
#endif

u32 hmc7044_config2_data[] = {
0x000162,
0x000160
};

u32 hmc7044_config3_data[] = {
0x0001e0,
0x000160
};

u32 hmc7044_config4_data[] = {
0x000164	
};

u32 hmc7044_config5_data[] = {
0x000160	
};


//#define ADC70M 1  //0db gain
#define ADC140M 1   //6db gain
//#define ADC1G 1

u32 adc_data[] = {    
		0x000081,
		//waite_time":"20000
		0x100         ,
		0x200         ,
		0x803         ,
		0xa00         ,
		0xf00         ,
		0x3f80        ,
		0x4080        ,
		0x4100        ,
		0x42ff        ,
		0x10800       ,
		0x10900       ,
		0x10a00       ,
		0x11000       ,
		0x11100       ,
		0x11200       ,
		0x11c00       ,
		0x11e00       ,
		0x1200c       ,
		0x12100       ,
		0x12200       ,
		0x12300       ,
		0x1ff01       ,
		0x20002       ,
		0x20103       ,
		0x27a00       ,
		0x30010       ,


#ifdef ADC70M
		0x31001       ,
		0x31170       ,
		0x31400       ,
		0x31500       ,
		0x0316c5,//FTW PAHSE0
		0x031792,
		0x03185f,
		0x03192c,
		0x031af9,
		0x031b05,//FTW PAHSE5
#endif
#ifdef ADC140M
		0x31041       ,
		0x31170       ,
		0x31400       ,
		0x31500       ,
		0x3168b       ,
		0x31725       ,
		0x318bf       ,
		0x31958       ,
		0x31af2       ,
		0x31b0b       ,
#endif
#ifdef ADC1G
		0x31041       ,
		0x31170       ,
		0x31400       ,
		0x31500       ,
		0x31655       ,
		0x31755       ,
		0x31855       ,
		0x31955       ,
		0x31a55       ,
		0x31b55       ,
#endif
		0x31d00       ,
		0x31e00       ,
		0x31f00       ,
		0x32000       ,
		0x32100       ,
		0x32200       ,
		0x32700       ,


#ifdef ADC70M
		0x33001       ,
		0x33105       ,
		0x33400       ,
		0x33500       ,
		0x0336c5,//FTW PAHSE0
		0x033792,
		0x03385f,
		0x03392c,
		0x033af9,
		0x033b05,//FTW PAHSE5
#endif
#ifdef ADC140M//140M
		0x33041       ,
		0x33105       ,
		0x33400       ,
		0x33500       ,
		0x3368b       ,
		0x33725       ,
		0x338bf       ,
		0x33958       ,
		0x33af2       ,
		0x33b0b       ,
#endif
#ifdef ADC1G//1G
		0x33041       ,
		0x33105       ,
		0x33400       ,
		0x33500       ,
		0x033655,//FTW PAHSE0
		0x033755,
		0x033855,
		0x033955,
		0x033a55,
		0x033b55,//FTW PAHSE5
#endif
		0x33d00       ,
		0x33e00       ,
		0x33f00       ,
		0x34000       ,
		0x34100       ,
		0x34200       ,
		0x34700       ,
		0x39000       ,
		0x39100       ,
		0x39200       ,
		0x39300       ,
		0x39400       ,
		0x39500       ,
		0x3980f       ,
		0x39900       ,
		0x39a00       ,
		0x39b00       ,
		0x39c00       ,
		0x39d00       ,
		0x30000       ,
		0x57115       ,
		0x55000       ,
		0x551b1       ,
		0x552b2       ,
		0x553b3       ,
		0x554b4       ,
		0x555b5       ,
		0x556b6       ,
		0x557b7       ,
		0x558b8       ,
		0x55900       ,
		0x55a00       ,
		0x56101       ,
		0x56200       ,
		0x56e10       ,
		0x570ff       ,
		0x57200       ,
		0x57300       ,
		0x57400       ,
		0x5781c       ,
		0x58000       ,
		0x58100       ,
		0x58300       ,
		0x58401       ,
		0x58502       ,
		0x58603       ,
		0x58704       ,
		0x58805       ,
		0x58906       ,
		0x58a07       ,
		0x58b87       ,
		0x58c00       ,
		0x58d1f       ,
		0x58e03       ,
		0x58f0f       ,
		0x5902f       ,
		0x5b0ff       ,
		0x5b267       ,
		0x5b345       ,
		0x5b523       ,
		0x5b601       ,
		0x5bf00       ,
		0x5b000       ,
		0x56400       ,
		0x5c011       ,
		0x5c111       ,
		0x5c211       ,
		0x5c311       ,
		0x5c480       ,
		0x5c580       ,
		0x5c680       ,
		0x5c780       ,
		0x5c880       ,
		0x5c980       ,
		0x5ca80       ,
		0x5cb80       ,
		0x12284f      ,
		0x12280f      ,
		0x122200      ,
		0x122204      ,
		0x122200      ,
		0x126208      ,
		0x126200      ,
		0x57114       ,
		0x801         ,
		0xdf802       ,
		0xdf900       ,
		0x802         ,
		0xdf802       ,
		0xdf900       ,
		0x801         ,
		0xdf802       ,
		0xdf900       ,

#ifdef ADC70M
		0x0e0055,
		0x0e0115,
		0x0e0244,
		0x0e0306,
		0x0e04c9,
		0x0e0505,
		0x0e0636,
		0x0e0704,
		0x0e088b,
		0x0e0901,
		0x0e0ae6,
		0x0e0bfd,
		0x0e0c92,
		0x0e0df9,
		0x0e0efd,
		0x0e0ff4,
		0x0e10b3,
		0x0e11f0,
		0x0e1258,
		0x0e13ed,
		0x0e1490,
		0x0e15eb,
		0x0e16f0,
		0x0e17eb,
		0x0e18ed,
		0x0e19ee,
		0x0e1ad2,
		0x0e1bf4,
		0x0e1cad,
		0x0e1dfd,
		0x0e1e43,
		0x0e1f09,
		0x0e2010,
		0x0e2117,
		0x0e226c,
		0x0e2326,
		0x0e245d,
		0x0e2536,
		0x0e26e4,
		0x0e2745,
		0x0e28ee,
		0x0e2953,
		0x0e2a7c,
		0x0e2b5f,
		0x0e2cb3,
		0x0e2d67,
		0x0e2ef8,
		0x0e2f6b,
		0x0f00f8,
		0x0f016b,
		0x0f02b3,
		0x0f0367,
		0x0f047c,
		0x0f055f,
		0x0f06ee,
		0x0f0753,
		0x0f08e4,
		0x0f0945,
		0x0f0a5d,
		0x0f0b36,
		0x0f0c6c,
		0x0f0d26,
		0x0f0e10,
		0x0f0f17,
		0x0f1043,
		0x0f1109,
		0x0f12ad,
		0x0f13fd,
		0x0f14d2,
		0x0f15f4,
		0x0f16ed,
		0x0f17ee,
		0x0f18f0,
		0x0f19eb,
		0x0f1a90,
		0x0f1beb,
		0x0f1c58,
		0x0f1ded,
		0x0f1eb3,
		0x0f1ff0,
		0x0f20fd,
		0x0f21f4,
		0x0f2292,
		0x0f23f9,
		0x0f24e6,
		0x0f25fd,
		0x0f268b,
		0x0f2701,
		0x0f2836,
		0x0f2904,
		0x0f2ac9,
		0x0f2b05,
		0x0f2c44,
		0x0f2d06,
		0x0f2e55,
		0x0f2f15,
#endif
#ifdef ADC140M//140M
		0x0e0077      ,
		0x0e0100      ,
		0x0e02d6      ,
		0x0e0300      ,
		0x0e04b6      ,
		0x0e0500      ,
		0x0e0670      ,
		0x0e0700      ,
		0x0e0865      ,
		0x0e09ff      ,
		0x0e0a43      ,
		0x0e0bfe      ,
		0x0e0c43      ,
		0x0e0dfd      ,
		0x0e0e56      ,
		0x0e0ffd      ,
		0x0e10c4      ,
		0x0e11fe      ,
		0x0e129c      ,
		0x0e1301      ,
		0x0e14e6      ,
		0x0e1504      ,
		0x0e163b      ,
		0x0e1707      ,
		0x0e18f0      ,
		0x0e1906      ,
		0x0e1a1d      ,
		0x0e1b03      ,
		0x0e1c1e      ,
		0x0e1dfc      ,
		0x0e1e0c      ,
		0x0e1ff4      ,
		0x0e204c      ,
		0x0e21ee      ,
		0x0e22a7      ,
		0x0e23ee      ,
		0x0e24e2      ,
		0x0e25f7      ,
		0x0e2680      ,
		0x0e270a      ,
		0x0e281c      ,
		0x0e2924      ,
		0x0e2abf      ,
		0x0e2b3f      ,
		0x0e2c22      ,
		0x0e2d57      ,
		0x0e2e89      ,
		0x0e2f64      ,
		0x0f0089      ,
		0x0f0164      ,
		0x0f0222      ,
		0x0f0357      ,
		0x0f04bf      ,
		0x0f053f      ,
		0x0f061c      ,
		0x0f0724      ,
		0x0f0880      ,
		0x0f090a      ,
		0x0f0ae2      ,
		0x0f0bf7      ,
		0x0f0ca7      ,
		0x0f0dee      ,
		0x0f0e4c      ,
		0x0f0fee      ,
		0x0f100c      ,
		0x0f11f4      ,
		0x0f121e      ,
		0x0f13fc      ,
		0x0f141d      ,
		0x0f1503      ,
		0x0f16f0      ,
		0x0f1706      ,
		0x0f183b      ,
		0x0f1907      ,
		0x0f1ae6      ,
		0x0f1b04      ,
		0x0f1c9c      ,
		0x0f1d01      ,
		0x0f1ec4      ,
		0x0f1ffe      ,
		0x0f2056      ,
		0x0f21fd      ,
		0x0f2243      ,
		0x0f23fd      ,
		0x0f2443      ,
		0x0f25fe      ,
		0x0f2665      ,
		0x0f27ff      ,
		0x0f2870      ,
		0x0f2900      ,
		0x0f2ab6      ,
		0x0f2b00      ,
		0x0f2cd6      ,
		0x0f2d00      ,
		0x0f2e77      ,
		0x0f2f00      ,
#endif

#ifdef ADC1G//1G
		0x0e001e,
		0x0e0100,
		0x0e0202,
		0x0e0300,
		0x0e04e1,
		0x0e05ff,
		0x0e0645,
		0x0e0700,
		0x0e08a2,
		0x0e09ff,
		0x0e0a4f,
		0x0e0b00,
		0x0e0cf9,
		0x0e0dff,
		0x0e0e91,
		0x0e0fff,
		0x0e10e9,
		0x0e1100,
		0x0e12dd,
		0x0e13fe,
		0x0e14e1,
		0x0e1500,
		0x0e16f5,
		0x0e17ff,
		0x0e18ce,
		0x0e19fe,
		0x0e1a61,
		0x0e1b02,
		0x0e1c1e,
		0x0e1dfd,
		0x0e1e2c,
		0x0e1f02,
		0x0e20f2,
		0x0e21ff,
		0x0e22e9,
		0x0e23fc,
		0x0e2443,
		0x0e2506,
		0x0e2602,
		0x0e27f8,
		0x0e2895,
		0x0e2906,
		0x0e2af0,
		0x0e2bff,
		0x0e2c38,
		0x0e2df0,
		0x0e2e68,
		0x0e2f4d,
		0x0f0068,
		0x0f014d,
		0x0f0238,
		0x0f03f0,
		0x0f04f0,
		0x0f05ff,
		0x0f0695,
		0x0f0706,
		0x0f0802,
		0x0f09f8,
		0x0f0a43,
		0x0f0b06,
		0x0f0ce9,
		0x0f0dfc,
		0x0f0ef2,
		0x0f0fff,
		0x0f102c,
		0x0f1102,
		0x0f121e,
		0x0f13fd,
		0x0f1461,
		0x0f1502,
		0x0f16ce,
		0x0f17fe,
		0x0f18f5,
		0x0f19ff,
		0x0f1ae1,
		0x0f1b00,
		0x0f1cdd,
		0x0f1dfe,
		0x0f1ee9,
		0x0f1f00,
		0x0f2091,
		0x0f21ff,
		0x0f22f9,
		0x0f23ff,
		0x0f244f,
		0x0f2500,
		0x0f26a2,
		0x0f27ff,
		0x0f2845,
		0x0f2900,
		0x0f2ae1,
		0x0f2bff,
		0x0f2c02,
		0x0f2d00,
		0x0f2e1e,
		0x0f2f00,
#endif
		0xf01         ,
		0x802         ,
		0xdf802       ,
		0xdf900       ,

#ifdef ADC70M
		0x0e0055,
		0x0e0115,
		0x0e0244,
		0x0e0306,
		0x0e04c9,
		0x0e0505,
		0x0e0636,
		0x0e0704,
		0x0e088b,
		0x0e0901,
		0x0e0ae6,
		0x0e0bfd,
		0x0e0c92,
		0x0e0df9,
		0x0e0efd,
		0x0e0ff4,
		0x0e10b3,
		0x0e11f0,
		0x0e1258,
		0x0e13ed,
		0x0e1490,
		0x0e15eb,
		0x0e16f0,
		0x0e17eb,
		0x0e18ed,
		0x0e19ee,
		0x0e1ad2,
		0x0e1bf4,
		0x0e1cad,
		0x0e1dfd,
		0x0e1e43,
		0x0e1f09,
		0x0e2010,
		0x0e2117,
		0x0e226c,
		0x0e2326,
		0x0e245d,
		0x0e2536,
		0x0e26e4,
		0x0e2745,
		0x0e28ee,
		0x0e2953,
		0x0e2a7c,
		0x0e2b5f,
		0x0e2cb3,
		0x0e2d67,
		0x0e2ef8,
		0x0e2f6b,
		0x0f00f8,
		0x0f016b,
		0x0f02b3,
		0x0f0367,
		0x0f047c,
		0x0f055f,
		0x0f06ee,
		0x0f0753,
		0x0f08e4,
		0x0f0945,
		0x0f0a5d,
		0x0f0b36,
		0x0f0c6c,
		0x0f0d26,
		0x0f0e10,
		0x0f0f17,
		0x0f1043,
		0x0f1109,
		0x0f12ad,
		0x0f13fd,
		0x0f14d2,
		0x0f15f4,
		0x0f16ed,
		0x0f17ee,
		0x0f18f0,
		0x0f19eb,
		0x0f1a90,
		0x0f1beb,
		0x0f1c58,
		0x0f1ded,
		0x0f1eb3,
		0x0f1ff0,
		0x0f20fd,
		0x0f21f4,
		0x0f2292,
		0x0f23f9,
		0x0f24e6,
		0x0f25fd,
		0x0f268b,
		0x0f2701,
		0x0f2836,
		0x0f2904,
		0x0f2ac9,
		0x0f2b05,
		0x0f2c44,
		0x0f2d06,
		0x0f2e55,
		0x0f2f15,
#endif
#ifdef ADC140M//140M
		0x0e0077      ,
		0x0e0100      ,
		0x0e02d6      ,
		0x0e0300      ,
		0x0e04b6      ,
		0x0e0500      ,
		0x0e0670      ,
		0x0e0700      ,
		0x0e0865      ,
		0x0e09ff      ,
		0x0e0a43      ,
		0x0e0bfe      ,
		0x0e0c43      ,
		0x0e0dfd      ,
		0x0e0e56      ,
		0x0e0ffd      ,
		0x0e10c4      ,
		0x0e11fe      ,
		0x0e129c      ,
		0x0e1301      ,
		0x0e14e6      ,
		0x0e1504      ,
		0x0e163b      ,
		0x0e1707      ,
		0x0e18f0      ,
		0x0e1906      ,
		0x0e1a1d      ,
		0x0e1b03      ,
		0x0e1c1e      ,
		0x0e1dfc      ,
		0x0e1e0c      ,
		0x0e1ff4      ,
		0x0e204c      ,
		0x0e21ee      ,
		0x0e22a7      ,
		0x0e23ee      ,
		0x0e24e2      ,
		0x0e25f7      ,
		0x0e2680      ,
		0x0e270a      ,
		0x0e281c      ,
		0x0e2924      ,
		0x0e2abf      ,
		0x0e2b3f      ,
		0x0e2c22      ,
		0x0e2d57      ,
		0x0e2e89      ,
		0x0e2f64      ,
		0x0f0089      ,
		0x0f0164      ,
		0x0f0222      ,
		0x0f0357      ,
		0x0f04bf      ,
		0x0f053f      ,
		0x0f061c      ,
		0x0f0724      ,
		0x0f0880      ,
		0x0f090a      ,
		0x0f0ae2      ,
		0x0f0bf7      ,
		0x0f0ca7      ,
		0x0f0dee      ,
		0x0f0e4c      ,
		0x0f0fee      ,
		0x0f100c      ,
		0x0f11f4      ,
		0x0f121e      ,
		0x0f13fc      ,
		0x0f141d      ,
		0x0f1503      ,
		0x0f16f0      ,
		0x0f1706      ,
		0x0f183b      ,
		0x0f1907      ,
		0x0f1ae6      ,
		0x0f1b04      ,
		0x0f1c9c      ,
		0x0f1d01      ,
		0x0f1ec4      ,
		0x0f1ffe      ,
		0x0f2056      ,
		0x0f21fd      ,
		0x0f2243      ,
		0x0f23fd      ,
		0x0f2443      ,
		0x0f25fe      ,
		0x0f2665      ,
		0x0f27ff      ,
		0x0f2870      ,
		0x0f2900      ,
		0x0f2ab6      ,
		0x0f2b00      ,
		0x0f2cd6      ,
		0x0f2d00      ,
		0x0f2e77      ,
		0x0f2f00      ,
#endif

#ifdef ADC1G//1G
		0x0e001e,
		0x0e0100,
		0x0e0202,
		0x0e0300,
		0x0e04e1,
		0x0e05ff,
		0x0e0645,
		0x0e0700,
		0x0e08a2,
		0x0e09ff,
		0x0e0a4f,
		0x0e0b00,
		0x0e0cf9,
		0x0e0dff,
		0x0e0e91,
		0x0e0fff,
		0x0e10e9,
		0x0e1100,
		0x0e12dd,
		0x0e13fe,
		0x0e14e1,
		0x0e1500,
		0x0e16f5,
		0x0e17ff,
		0x0e18ce,
		0x0e19fe,
		0x0e1a61,
		0x0e1b02,
		0x0e1c1e,
		0x0e1dfd,
		0x0e1e2c,
		0x0e1f02,
		0x0e20f2,
		0x0e21ff,
		0x0e22e9,
		0x0e23fc,
		0x0e2443,
		0x0e2506,
		0x0e2602,
		0x0e27f8,
		0x0e2895,
		0x0e2906,
		0x0e2af0,
		0x0e2bff,
		0x0e2c38,
		0x0e2df0,
		0x0e2e68,
		0x0e2f4d,
		0x0f0068,
		0x0f014d,
		0x0f0238,
		0x0f03f0,
		0x0f04f0,
		0x0f05ff,
		0x0f0695,
		0x0f0706,
		0x0f0802,
		0x0f09f8,
		0x0f0a43,
		0x0f0b06,
		0x0f0ce9,
		0x0f0dfc,
		0x0f0ef2,
		0x0f0fff,
		0x0f102c,
		0x0f1102,
		0x0f121e,
		0x0f13fd,
		0x0f1461,
		0x0f1502,
		0x0f16ce,
		0x0f17fe,
		0x0f18f5,
		0x0f19ff,
		0x0f1ae1,
		0x0f1b00,
		0x0f1cdd,
		0x0f1dfe,
		0x0f1ee9,
		0x0f1f00,
		0x0f2091,
		0x0f21ff,
		0x0f22f9,
		0x0f23ff,
		0x0f244f,
		0x0f2500,
		0x0f26a2,
		0x0f27ff,
		0x0f2845,
		0x0f2900,
		0x0f2ae1,
		0x0f2bff,
		0x0f2c02,
		0x0f2d00,
		0x0f2e1e,
		0x0f2f00,
#endif
		0xf01         ,
		0x803         ,
		0x18e300      ,
		0x190800      ,
		0x18a600      ,
		0x18e641      ,
		0x70186       ,
		0x191008      ,
		0x1a4c09      ,
		0x1a4d09      ,
		0x24810       ,
		0x24700       ,
		0x24a10       ,
		0x24900       ,
		0x24c04       ,
		0x24b04       ,
		0x24501       ,
		//0x812800,//SYSREF hold status.
0x812800
};

u32 dac_config1_data[] = {
0x000081,
0x000000,
0x009100,
0x020601,
0x070501,
0x009000,
0x009501,
0x0790ff,
0x0791ff,
0x00C000,
0x00DB00,
0x00DB01,
0x00DB00,
0x00C148,
0x00C149,
0x00C701,
0x80C300,  //bit0=1婵炴垶鎸鹃崕銈夘敆濠婂懏鏆滈柨鐕傛嫹
0x00502A,
0x006168,
0x005182,
0x005183,
0x008103,
0x010000,
0x011028,
0x011181,
0x008400,
0x031200,
0x03000B,
0x047509,
0x045383,
0x04582F,
0x047501,
0x03000C,
0x047509,
0x045383,
0x04582F,
0x047501,
0x0008C0,
0x01120C,

#ifdef ADC70M
0x0114c5,//FTW PAHSE0
0x011592,
0x01165f,
0x01172c,
0x0118f9,
0x011905,//FTW PAHSE5
#endif

#ifdef ADC140M//140M
0x01148b,//FTW phase0
0x011525,
0x0116bf,
0x011758,
0x0118f2,
0x01190b,//FTW phase5
#endif

#ifdef ADC1G//1G
0x011455,//FTW phase0
0x011555,
0x011655,
0x011755,
0x011855,
0x011955,//FTW phase5
#endif
0x011C00,
0x011D00,
0x012400,
0x012500,
0x012600,
0x012700,
0x012800,
0x012900,
0x012A00,
0x012B00,
0x012C00,
0x012D00,
0x012E00,
0x012F00,
0x011301,
0x0240AA,
0x0241AA,
0x024255,
0x024355,
0x02441F,
0x02451F,
0x02461F,
0x02471F,
0x02481F,
0x02491F,
0x024A1F,
0x024B1F,
0x020100,
0x020300,
0x025301,
0x025401,
0x021016,
0x021605,
0x0212FF,
0x021200,
0x021087,
0x021611,
0x021301,
0x021300,
0x020000,
// waite 100ms   
0x021086,
0x021640,
0x021301,
0x021300,
0x021086,
0x021600,
0x021301,
0x021300,
0x021087,
0x021601,
0x021301,
0x021300,
0x028005,
0x028001,
0x828100,  //bit0=1婵炴垶鎸鹃崕銈夘敆濠婂懏鏆滈柨鐕傛嫹
0x030808,
0x03091a,
0x030A2c,
0x030B3e,
0x03060C,
0x03070C,
0x030401,
0x030501,
0x003BF1,
0x003A02
};

u32 dac_config2_data[] = {
0x03000B,
0x008513,
0x01DE00,
0x0008C0,
0x05960C
};


void zeros_buf(u8 *buf, int length){
	int i=0;
	for(i=0;i<length;i++){
		*(buf+i) = 0;
	}
}

void set_buf(u8 *buf, int length, u32 val){
	zeros_buf(buf,length);
	int i;
	u32 mask_0 = 0xff;
	for(i=0;i<length;i++){
		u32 mask = mask_0 << (i*8);
		u32 data = (val & mask);
		data = data >> (i*8);
		buf[length-1-i] = (u8)(data);
	}
}

u8 spi_read(u32 base_addr, u32 data, u32 slave_mask){
	gw_sleep(DELAY_WR);
	u8 wb[3] = { 0,0,0}; //{cmd,addr,data}
	u8 rb[3] = { 0,0,0};

	set_buf(wb,3,data);
	int status = xSpi_trans(slave_mask, wb, rb, 3);
	if(status != 0){
		gw_print(" !!!!! Spi read error : status = %d \r\n", status);
		return -1;
	}
	// spi_trans(base_addr, slave_mask, wb, rb, 3, 0);
	gw_print("Spi read : wb -- 0x%02x%02x%02x, rb -- 0x%02x%02x%02x \r\n", wb[0],wb[1],wb[2],rb[0],rb[1],rb[2]);
	return rb[2];
}

void spi_write(u32 base_addr, u32 data, u32 slave_mask){
	gw_sleep(DELAY_WR);
	u8 wb[3] = { 0,0,0}; //{cmd(1bit),addr(7bit),data(16bit)}
	u8 rb[3] = { 0,0,0};
	set_buf(wb,3,data);
	int status = xSpi_trans(slave_mask, wb, rb, 3);
	if(status != 0){
		gw_print(" !!!!! Spi write error : status = %d \r\n", status);
		return;
	}
	// spi_trans(base_addr, slave_mask, wb, rb, 3, 0);
	// gw_print(" --------- Spi write : wb -- 0x%02x%02x%02x, rb -- 0x%02x%02x%02x \r\n", wb[0],wb[1],wb[2],rb[0],rb[1],rb[2]);
}

int hmc7044_config1(){
	int i,len;
	len = sizeof(hmc7044_config1_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, hmc7044_config1_data[i], HMC7044_SPI_SS);
	}
	gw_print("wait hmc7044_config1 ..... \r\n");
	return 0;
}

int hmc7044_config2(){
	int i,len;
	len = sizeof(hmc7044_config2_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, hmc7044_config2_data[i], HMC7044_SPI_SS);
	}
	gw_print("wait hmc7044_config2 ..... \r\n");
	return 0;
}

int hmc7044_config3(){
	int i,len;
	len = sizeof(hmc7044_config3_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, hmc7044_config3_data[i], HMC7044_SPI_SS);
	}
	gw_print("wait hmc7044_config3 ..... \r\n");
	return 0;
}

int hmc7044_config4(){
	int i,len;
	len = sizeof(hmc7044_config4_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, hmc7044_config4_data[i], HMC7044_SPI_SS);
	}
	gw_print("wait hmc7044_config4 ..... \r\n");
	return 0;
}

int hmc7044_config5(){
	int i,len;
	len = sizeof(hmc7044_config5_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, hmc7044_config5_data[i], HMC7044_SPI_SS);
	}
	gw_print("wait hmc7044_config5 ..... \r\n");
	return 0;
}



#define delay_test 100

/* 3 wire to 4 wire configure */
int adc_config(){
	int i,len;
	len = sizeof(adc_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, adc_data[i], ADC_SPI_SS);
		if(adc_data[i] == 0x000081) {
			gw_sleep(DELAY_CNT); // 20ms
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			gw_sleep(DELAY_CNT);
			//gw_sleep(DELAY_CNT); // 20ms
		}
	}

	gw_print("wait adc_config ..... \r\n");

	return 0;
}

int config_dac_config1_data(){
	int i,len;
	len = sizeof(dac_config1_data) / sizeof(u32);

	for(i=0;i<len;i++){
		if(dac_config1_data[i] == 0x80C300){
			u8 rx_data = spi_read(XPAR_AXI_SPI_BASEADDR, 0x80C300, DAC_SPI_SS);
			u8 data = (rx_data & 0x1);
			if(data != 0x1){
				return -1;
			}
		}else if(dac_config1_data[i] == 0x828100){
			u8 rx_data = spi_read(XPAR_AXI_SPI_BASEADDR, 0x828100, DAC_SPI_SS);
			u8 data = (rx_data & 0x1);
			if(data != 0x1){
				return -1;
			}
		}
		spi_write(XPAR_AXI_SPI_BASEADDR, dac_config1_data[i], DAC_SPI_SS);
		
		if(dac_config1_data[i] == 0x020000) {
			gw_sleep(DELAY_CNT); // 100ms
		}
	}

	gw_print("wait dac_config1_data ..... \r\n");
	return 0;
}

int config_dac_config2_data(){
	int i,len;
	len = sizeof(dac_config2_data) / sizeof(u32);

	for(i=0;i<len;i++){
		spi_write(XPAR_AXI_SPI_BASEADDR, dac_config2_data[i], DAC_SPI_SS);
	}
	gw_print("wait dac_config2_data ..... \r\n");
	return 0;
}

void reset_204B(){
    set_gpio(O_SOFT_RST);
	gw_sleep(DELAY_CNT);
    reset_gpio(O_SOFT_RST);
}

int spi_configure(){

	gw_print(" \r\n ****** start spi_configure ****** \r\n");

	if (read_gpio(I_PG_DONE) != 1) {
		gw_print("I_PG_DONE != 1, power failed. \r\n");
		return -1;
	}

	u8 freq_7044 = spi_read(XPAR_AXI_SPI_BASEADDR,0x810500, HMC7044_SPI_SS); // 0x010501
	if( (freq_7044 != 0x01) | !(read_gpio(I_HMC7044_PLL2LOCK))){
		gpio_startup();

		gw_sleep(delay_test);
		hmc7044_config1();
		gw_sleep(delay_test);
		hmc7044_config2();
		gw_print("hmc7044_pll2_lock_check\r\n");
		gw_sleep(delay_test);

		/* check gpio */
		if( !(read_gpio(I_HMC7044_PLL2LOCK)) ){
			gw_print("hmc7044_pll2_locked failed \r\n");
			return -1;
		}

		if( !(read_gpio(I_HMC7044_PLL1LOCK)) ){
			gw_print("hmc7044_pll1 is not working !\r\n");
		}

		hmc7044_config3();

		gw_sleep(delay_test);
		/* check 1 gpio */
		if( !read_gpio(I_HMC7044_PHASE)){
			gw_print("hmc7044_phase_locked failed \r\n");
			return -1;
		}

		/* reset via gpio */
		set_gpio(O_HMC7044_SYNC);

		gw_sleep(1000);

		HwIcaptest();

	}

	reset_204B();

	gw_sleep(delay_test);
	gw_print("config_ADC\r\n");
	gw_sleep(delay_test);

	adc_config();
	gw_sleep(delay_test);


	while(config_dac_config1_data() == -1){
		gw_sleep(DELAY_CNT);
		// post event to eventHandler();
	}
	gw_sleep(delay_test);
	hmc7044_config4();
	config_dac_config2_data();
	gw_sleep(delay_test);
	reset_204B();

	hmc7044_config5();
	gw_sleep(delay_test);
	/* check gpio */


	#if 1
	if( (read_gpio(I_TX1_SYNC) != 1)){
		return -1;
	}
	if( (read_gpio(I_TX2_SYNC) != 1)){
		return -1;
	}
	#endif


	// if( (read_gpio(I_RX_SYNC) != 1)){
	// 	return -1;
	// }
	u32 value_adc_sync = gw_ReadReg(0x44a20038);
	if(value_adc_sync != 0x00010001){
		return -1;
	}

	return 0;
}



static void spiConfigureTask( void *pvParameters )
{
	QueueHandle_t xEventQueue = (QueueHandle_t)pvParameters;
	reset_gpio(O_LED2);
	for( ;; )
	{
		if(spi_configure() == 0){
			break;
		}
	}

	set_gpio(O_LED2);

    Event event;
    makeEvent(&event, 2, EVENT_CONFIGURE_OK, NULL);
    xQueueSend( xEventQueue, &event, 0UL );

	vTaskDelete( xSpiConfigureTask );
}

int createSpiConfigureTask(QueueHandle_t xEventQueue){
    BaseType_t xReturn = xTaskCreate( spiConfigureTask, 
	                                  (const char *) "spiConfigureTask", 
									  configMINIMAL_STACK_SIZE * 2, 
									  xEventQueue, 
									  tskIDLE_PRIORITY + 3, /* gw_priority */
									  &xSpiConfigureTask);
	if(xReturn != pdPASS){
		return -1;
	}
	return 0;
}


/*************************** cli ****************************/
int cli_spi_read(u32 data, u32 slave_mask, u8 *rx_data){
	u8 wb[3] = { 0,0,0}; //{cmd,addr,data}
	u8 rb[3] = { 0,0,0};

	set_buf(wb,3,data);
	int status = xSpi_trans(slave_mask, wb, rb, 3);
	if(status != 0){
		// gw_print(" !!!!! Spi read error : status = %d \r\n", status);
		return -1;
	}
	*rx_data = rb[2];
	return 0;
}

int cli_spi_write(u32 data, u32 slave_mask){
	u8 wb[3] = { 0,0,0}; //{cmd(1bit),addr(7bit),data(16bit)}
	u8 rb[3] = { 0,0,0};
	set_buf(wb,3,data);
	int status = xSpi_trans(slave_mask, wb, rb, 3);
	if(status != 0){
		return -1;
	}
	return 0;
}

int monitor_204b(){
	if( (read_gpio(I_TX1_SYNC) != 1)){
		return -1;
	}
	if( (read_gpio(I_TX2_SYNC) != 1)){
		return -1;
	}
	u32 value_adc_sync = gw_ReadReg(0x44a20038);
	if(value_adc_sync != 0x00010001){
		return -1;
	}
	return 0;
}

